package com.ruoyi.web.controller.pv;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.doudou.core.container.ListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.IterableUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pv.domain.PvTrainingProgram;
import com.ruoyi.pv.service.IPvTrainingProgramService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 实训项目Controller
 *
 * @author zzx
 * @date 2023-03-12
 */
@Api("实训项目")
@RestController
@RequestMapping("/pv/program")
public class PvTrainingProgramController extends BaseController {

    @Autowired private IPvTrainingProgramService pvTrainingProgramService;

    /**
     * 查询实训项目列表
     */

    @GetMapping("/list")
    @ApiOperation("查询实训项目列表")
    public TableDataInfo list(PvTrainingProgram pvTrainingProgram) {
        startPage();
        pvTrainingProgram.setReservedOne(getDeptId());
        List<PvTrainingProgram> list = pvTrainingProgramService.selectPvTrainingProgramList(pvTrainingProgram);
        return getDataTable(list);
    }

    /**
     * 导出实训项目列表
     */
    @PreAuthorize("@ss.hasPermi('pv:program:export')")
    @Log(title = "实训项目" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PvTrainingProgram pvTrainingProgram) {
        pvTrainingProgram.setReservedOne(getDeptId());
        List<PvTrainingProgram> list = pvTrainingProgramService.selectPvTrainingProgramList(pvTrainingProgram);
        ExcelUtil<PvTrainingProgram> util = new ExcelUtil<PvTrainingProgram>(PvTrainingProgram.class);
        util.exportExcel(response, list, "实训项目数据");
    }

    /**
     * 导出模板
     */
    @PreAuthorize("@ss.hasPermi('pv:program:importModel')")
    @Log(title = "实训项目模板导出" , businessType = BusinessType.EXPORT)
    @PostMapping("/importModel")
    public void importModel(HttpServletResponse response) throws Exception {
        List<PvTrainingProgram> list = ListUtil.list(false);
        ExcelUtil<PvTrainingProgram> util = new ExcelUtil<PvTrainingProgram>(PvTrainingProgram.class);
        util.exportExcel(response, list, "实训项目模板");
    }

    /**
     * 导入实训项目列表
     */
    @PreAuthorize("@ss.hasPermi('pv:program:import')")
    @Log(title = "实训项目导入" , businessType = BusinessType.EXPORT)
    @PostMapping("/import")
    public AjaxResult importExcel(MultipartFile file) throws Exception {
        ExcelUtil<PvTrainingProgram> util = new ExcelUtil<>(PvTrainingProgram.class);
        List<PvTrainingProgram> programs = util.importExcel(file.getInputStream());
        if (!IterableUtils.isEmpty(programs)){
            Long deptId = getDeptId();
            for (PvTrainingProgram program : programs) {
                program.setReservedOne(deptId);
            }
        }
        return toAjax(pvTrainingProgramService.insertPvTrainingProgramAll(programs));
    }


    /**
     * 获取实训项目详细信息
     */

    @GetMapping(value = "/{id}")
    @ApiOperation("查询培训班管理列表")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pvTrainingProgramService.selectPvTrainingProgramById(id));
    }

    /**
     * 新增实训项目
     */
    @PreAuthorize("@ss.hasPermi('pv:program:add')")
    @Log(title = "实训项目" , businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增实训项目")
    public AjaxResult add(@RequestBody PvTrainingProgram pvTrainingProgram) {
        pvTrainingProgram.setReservedOne(getDeptId());
        return toAjax(pvTrainingProgramService.insertPvTrainingProgram(pvTrainingProgram));
    }

    /**
     * 修改实训项目
     */
    @PreAuthorize("@ss.hasPermi('pv:program:edit')")
    @Log(title = "实训项目" , businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改实训项目")
    public AjaxResult edit(@RequestBody PvTrainingProgram pvTrainingProgram) {
        return toAjax(pvTrainingProgramService.updatePvTrainingProgram(pvTrainingProgram));
    }

    /**
     * 删除实训项目
     */
    @PreAuthorize("@ss.hasPermi('pv:program:remove')")
    @Log(title = "实训项目" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除实训项目")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pvTrainingProgramService.deletePvTrainingProgramByIds(ids));
    }
}
